#include "stdafx.h"

namespace cpputils
{

smart_file::smart_file(FILE* V)
	: Ptr(V)
{
}

smart_file::smart_file(const smart_file& V)
	: Ptr(V.release())
{
}

smart_file::smart_file(const string& Filename, const string& Mode)
	: Ptr(checked_fopen(Filename, Mode))
{
}

smart_file::operator FILE*() const
{
	return Ptr;
}

smart_file& smart_file::operator=(const smart_file& V)
{
	reset(V.release());
	return *this;
}

smart_file::~smart_file()
{
	reset();
}

FILE* smart_file::get() const
{
	return Ptr;
}

FILE* smart_file::release() const
{
	FILE* Temp = Ptr;
	Ptr = 0;
	return Temp;
}

void smart_file::reset(FILE* V)
{
	if (V != Ptr && Ptr != 0 && fclose(Ptr) != 0)
		throw runtime_error("Can't close file");
	Ptr = V;
}

void smart_file::reset(const string& Filename, const string& Mode)
{
	reset();
	Ptr = checked_fopen(Filename, Mode);
}

}
